import {Component} from 'react';
import React from 'react';
import {connect} from 'umi';

import HighPerformTableSpecially from '@/components/HighPerformTableSpecially';
import {Button} from 'antd';
import {PlusOutlined} from '@ant-design/icons';
import APIS from '@/apis';
import FilterBySchool from '@/components/FilterBySchool';
import SearchAbleSelect from '@/components/SearchAbleSelect';

class CollegeManagerPage extends Component<any, any> {
  actionRef: any;


  constructor(props: any) {
    super(props);
    this.state = {
      allSchool: [],
      //collegeBySchool:[],
      schoolId: 0,
      schoolValue: 0,
      schoolName: ""
    }
  }

  componentDidMount() {
    APIS.OrgsAPI.GetSchoolList({current: 1, pageSize: 9999}).then((res) => {
      //console.log(res);
      this.setState({allSchool: res.data, schoolId: res.data[0].id, schoolName: res.data[0].name})
      setTimeout(() => {
        this.actionRef?.reload();
      }, 200);
    })
  }

  handleDelete(data: any) {
    return APIS.OrgsAPI.DeleteNewCollge(data)
      .then(() => {
        return Promise.resolve(true);
      })
      .catch(() => {
        return Promise.resolve(false);
      });
  }

  handleAdd(data: any) {
    if (!Object.getOwnPropertyNames(data).some(i => i === "school")) {
      data["school"] = this.state.schoolId;
    }
    return APIS.OrgsAPI.CreateNewCollge(data)
      .then(() => {
        return Promise.resolve(true);
      })
      .catch(() => {
        return Promise.resolve(false);
      });
  }

  handleUpdate(data: any) {
    //console.log(data);
    return APIS.OrgsAPI.UpdateCollege(data)
      .then(() => {
        return Promise.resolve(true);
      })
      .catch(() => {
        return Promise.resolve(false);
      });
  }


  requestGetData(
    params: {
      pageSize?: number;
      current?: number;
    },
    sorter: {
      [key: string]: 'ascend' | 'descend';
    },
    filter: {
      [key: string]: React.ReactText[];
    },
  ) {
    // return APIS.OrgsAPI.GetCollgeList({ ...params, sorter, filter,tag:this.state.schoolId });
    if (this.state.allSchool.length > 0) {
      //console.log(this.state.allSchool.length);
      return APIS.OrgsAPI.GetCollegeListBySchool({id: this.state.schoolId});
    } else return {};

  }

  handleChangeState(record: any, state: boolean) {
    return APIS.OrgsAPI.ChangeCollegeState({id: record.id, state: state ? 1 : 0}).then(
      (response) => {
        return Promise.resolve(response.data.state === 1);
      },
    );
  }

  clomns: any = [
    {
      title: '学院名称',
      dataIndex: 'collegeName',
      rules: [
        {
          required: true,
          message: '规则名称为必填项',
        },
      ],
    },
    {
      title: '学校名称',
      dataIndex: 'school',
      hideColumn: true,
      rules: [
        {
          required: true,
          message: '规则名称为必填项',
        },
      ],
      renderText: (dataIndex: any, record: any) => <span>{record.collegeData?.collegeName}</span>,
      renderFormItem: (item: any, config: any, form: any) => (
        <SearchAbleSelect
          searchData={async (key) => {
            var datas: Array<any> = await APIS.OrgsAPI.SearchGetSchoolList({current: 1, pageSize: 9999});
            return Promise.resolve(
              datas.map((item) => {
                return {value: item.id, key: item.name};
              }),
            );
          }}
          form={form}
          name="school"
          placeholder='请选择一个学校'
          // values={this.state.schoolValue}
          initialValue={this.state.schoolId}
          getValue={(value) => {
            this.setState({schoolValue: value.school});
          }}
        ></SearchAbleSelect>
      ),
    },
    {
      title: '更新时间',
      hideInForm: true,
      dataIndex: 'updatedTime',
      // valueType: 'datatime',
    },
  ];

  handleDataImport() {
  }

  handleDataExport() {
  }

  render() {
    return (
      <div>
        <FilterBySchool
          school={this.state.allSchool}
          schoolName={this.state.allSchool.length > 0 ? this.state.schoolName : null}
          getValue={(id, name) => {
            this.setState({schoolId: id, schoolValue: id, schoolName: name});
            this.actionRef?.reload();
          }}
        />
        <HighPerformTableSpecially<any>
          requestGetData={this.requestGetData.bind(this)}
          ref={(ref) => (this.actionRef = ref)}
          columns={this.clomns}
          hidePagination
          handleDelete={this.handleDelete.bind(this)}
          handleAdd={this.handleAdd.bind(this)}
          handleChangeState={this.handleChangeState.bind(this)}
          handleUpdate={this.handleUpdate.bind(this)}
          toolBarRender={() => [
            <Button type="primary" onClick={() => {
              this.actionRef.handleModalVisible(true);
            }}>
              <PlusOutlined/> 新增学院
            </Button>,
          ]}
        />
      </div>
    );
  }
}

export default connect()(CollegeManagerPage);
